package com.yanfan.energy.controller;

import com.yanfan.common.annotation.Log;
import com.yanfan.common.core.controller.BaseController;
import com.yanfan.common.core.domain.AjaxResult;
import com.yanfan.common.core.page.TableDataInfo;
import com.yanfan.common.enums.BusinessType;
import com.yanfan.common.utils.poi.ExcelUtil;
import com.yanfan.energy.domain.IbmsEnergyDevice;
import com.yanfan.energy.service.IIbmsEnergyDeviceService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.util.Arrays;
import java.util.List;

/**
 * 能耗设备Controller
 *
 * @author genluo
 * @date 2024-09-18
 */
@Api(tags = "能耗设备")
@RestController
@RequestMapping("/energy/device")
public class IbmsEnergyDeviceController extends BaseController {
    @Autowired
    private IIbmsEnergyDeviceService ibmsEnergyDeviceService;

    /**
     * 查询能耗设备列表
     */
    @ApiOperation("能耗设备分页列表")
    @PreAuthorize("@ss.hasPermi('energy:device:list')")
    @GetMapping("/list")
    public TableDataInfo list(IbmsEnergyDevice ibmsEnergyDevice) {
        startPage();
        List<IbmsEnergyDevice> list = ibmsEnergyDeviceService.queryPageList(ibmsEnergyDevice);
        return getDataTable(list);
    }

    /**
     * 导出能耗设备列表
     */
    @ApiOperation("导出能耗设备列表")
    @PreAuthorize("@ss.hasPermi('energy:device:export')")
    @Log(title = "能耗设备", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, IbmsEnergyDevice ibmsEnergyDevice) {
        List<IbmsEnergyDevice> list = ibmsEnergyDeviceService.queryPageList(ibmsEnergyDevice);
        ExcelUtil<IbmsEnergyDevice> util = new ExcelUtil<IbmsEnergyDevice>(IbmsEnergyDevice.class);
        util.exportExcel(response, list, "能耗设备数据");
    }

    /**
     * 获取能耗设备详细信息
     */
    @ApiOperation("获取能耗设备详细信息")
    @PreAuthorize("@ss.hasPermi('energy:device:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return success(ibmsEnergyDeviceService.getById(id));
    }

    /**
     * 新增能耗设备
     */
    @ApiOperation("新增能耗设备")
    @PreAuthorize("@ss.hasPermi('energy:device:add')")
    @Log(title = "能耗设备", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@Valid @RequestBody IbmsEnergyDevice ibmsEnergyDevice) {
        return toAjax(ibmsEnergyDeviceService.save(ibmsEnergyDevice));
    }

    /**
     * 修改能耗设备
     */
    @ApiOperation("修改能耗设备")
    @PreAuthorize("@ss.hasPermi('energy:device:edit')")
    @Log(title = "能耗设备", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@Valid @RequestBody IbmsEnergyDevice ibmsEnergyDevice) {
        return toAjax(ibmsEnergyDeviceService.updateById(ibmsEnergyDevice));
    }

    /**
     * 删除能耗设备
     */
    @ApiOperation("删除能耗设备")
    @PreAuthorize("@ss.hasPermi('energy:device:remove')")
    @Log(title = "能耗设备", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(ibmsEnergyDeviceService.removeBatchByIds(Arrays.asList(ids)));
    }
}
